import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/** A class used to change pictures displayed as certain objects in game
 *  @author crezax
 */

public class GraphicsOptions extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5019143180810011414L;
	private JPanel jContentPane = null;
	private JButton jChangeFirstAvatarButton = null;
	private JButton jChangeBoxButton = null;
	private JButton jChangeWallButton = null;
	private JButton jChangeFloorButton = null;
	private JButton jChangeHoleButton = null;
	private JButton jRestoreDefaultsButton = null;
	private JFileChooser myFiles;
	private Window window;
	private JButton jChangeSecondAvatarButton = null;
	private JLabel jFirstAvatarImage = null;
	private JLabel jSecondAvatarImage = null;
	private JLabel jBoxImage = null;
	private JLabel jWallImage = null;
	private JLabel jFloorImage = null;
	private JLabel jHoleImage = null;
	/**
	 * Constructor, that requires a Window instance to be given. If null reference is given and there is Window running, graphics in that window won't be updated until it's reset.
	 * 
	 */
	public GraphicsOptions(Window window) {
		super();
		this.window = window;
		initialize();
	}

	private JFrame getJFrame(){
		return this;
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
		this.myFiles = new JFileChooser();
		this.myFiles.setCurrentDirectory(new File("images"));
		this.setSize(new Dimension(437, 431));
		this.setTitle("Graphics Options");
		this.setContentPane(getJContentPane());
		this.setVisible(true);
		this.setFocusable(true);
	}
	
	private void updateFirstAvatarImage(){
		ImageIcon icon = new ImageIcon("images/player.jpg");
		icon.getImage().flush();
		jFirstAvatarImage.setIcon(icon);
	}
	
	private void updateSecondAvatarImage(){
		ImageIcon icon = new ImageIcon("images/player2.jpg");
		icon.getImage().flush();
		jSecondAvatarImage.setIcon(icon);
	}
	
	private void updateBoxImage(){
		ImageIcon icon = new ImageIcon("images/box.jpg");
		icon.getImage().flush();
		jBoxImage.setIcon(icon);
	}
	
	private void updateWallImage(){
		ImageIcon icon = new ImageIcon("images/wall.jpg");
		icon.getImage().flush();
		jWallImage.setIcon(icon);
	}
	
	private void updateFloorImage(){
		ImageIcon icon = new ImageIcon("images/floor.jpg");
		icon.getImage().flush();
		jFloorImage.setIcon(icon);
	}
	
	private void updateHoleImage(){
		ImageIcon icon = new ImageIcon("images/hole.jpg");
		icon.getImage().flush();
		jHoleImage.setIcon(icon);
	}

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
			gridBagConstraints31.gridx = 1;
			gridBagConstraints31.gridy = 8;
			jHoleImage = new JLabel();
			jHoleImage.setIcon(new ImageIcon("images/hole.jpg"));
			jHoleImage.setSize(40, 40);
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 1;
			gridBagConstraints21.gridy = 7;
			jFloorImage = new JLabel();
			jFloorImage.setIcon(new ImageIcon("images/floor.jpg"));
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 1;
			gridBagConstraints12.gridy = 6;
			jWallImage = new JLabel();
			jWallImage.setIcon(new ImageIcon("images/wall.jpg"));
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 1;
			gridBagConstraints7.gridy = 4;
			jBoxImage = new JLabel();
			jBoxImage.setIcon(new ImageIcon("images/box.jpg"));
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 1;
			gridBagConstraints6.gridy = 1;
			jSecondAvatarImage = new JLabel();
			jSecondAvatarImage.setIcon(new ImageIcon("images/player2.jpg"));
			GridBagConstraints gridBagConstraints51 = new GridBagConstraints();
			gridBagConstraints51.gridx = 1;
			gridBagConstraints51.gridheight = 1;
			gridBagConstraints51.gridwidth = 1;
			gridBagConstraints51.fill = GridBagConstraints.NONE;
			gridBagConstraints51.insets = new Insets(0, 0, 0, 0);
			gridBagConstraints51.weightx = 0.0;
			gridBagConstraints51.weighty = 0.0;
			gridBagConstraints51.ipadx = 0;
			gridBagConstraints51.ipady = 1;
			gridBagConstraints51.anchor = GridBagConstraints.CENTER;
			gridBagConstraints51.gridy = 0;
			jFirstAvatarImage = new JLabel();
			jFirstAvatarImage.setIcon(new ImageIcon("images/player.jpg"));
			jFirstAvatarImage.setSize(new Dimension(40,40));
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.gridy = 1;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 2;
			gridBagConstraints5.insets = new Insets(0, 25, 0, 0);
			gridBagConstraints5.gridy = 6;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.gridy = 8;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridy = 7;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.gridy = 6;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridy = 4;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.anchor = GridBagConstraints.CENTER;
			gridBagConstraints.gridy = 0;
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(getJChangeFirstAvatarButton(), gridBagConstraints);
			jContentPane.add(getJChangeBoxButton(), gridBagConstraints1);
			jContentPane.add(getJChangeWallButton(), gridBagConstraints2);
			jContentPane.add(getJChangeFloorButton(), gridBagConstraints3);
			jContentPane.add(getJChangeHoleButton(), gridBagConstraints4);
			jContentPane.add(getJRestoreDefaultsButton(), gridBagConstraints5);
			jContentPane.add(getJChangeSecondAvatarButton(), gridBagConstraints11);
			jContentPane.add(jFirstAvatarImage, gridBagConstraints51);
			jContentPane.add(jSecondAvatarImage, gridBagConstraints6);
			jContentPane.add(jBoxImage, gridBagConstraints7);
			jContentPane.add(jWallImage, gridBagConstraints12);
			jContentPane.add(jFloorImage, gridBagConstraints21);
			jContentPane.add(jHoleImage, gridBagConstraints31);
			jContentPane.setFocusable(true);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jChangeAvatarButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJChangeFirstAvatarButton() {
		if (jChangeFirstAvatarButton == null) {
			jChangeFirstAvatarButton = new JButton();
			jChangeFirstAvatarButton.setText("Change Avatar 1");
			jChangeFirstAvatarButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int result = myFiles.showOpenDialog(getJFrame());
					if (result == JFileChooser.APPROVE_OPTION) {
						BufferedImage tmpImage = null;
						try {
							tmpImage = ImageIO.read(myFiles.getSelectedFile());
						} catch (Exception f) {
							JOptionPane.showMessageDialog(getJFrame(), "Reading of image failed!", "ERROR", JOptionPane.ERROR_MESSAGE);
						}
						Field.setFirstAvatarImage(tmpImage);
						updateFirstAvatarImage();
						window.repaint();
					}
				}
			});
		}
		return jChangeFirstAvatarButton;
	}

	/**
	 * This method initializes jChangeBoxButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJChangeBoxButton() {
		if (jChangeBoxButton == null) {
			jChangeBoxButton = new JButton();
			jChangeBoxButton.setText("Change Box");
			jChangeBoxButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int result = myFiles.showOpenDialog(getJFrame());
					if (result == JFileChooser.APPROVE_OPTION) {
						BufferedImage tmpImage = null;
						try {
							tmpImage = ImageIO.read(myFiles.getSelectedFile());
						} catch (Exception f) {
							JOptionPane.showMessageDialog(getJFrame(), "Reading of image failed!", "ERROR", JOptionPane.ERROR_MESSAGE);
						}
						Field.setBoxImage(tmpImage);
						updateBoxImage();
						window.repaint();
					}
				}
			});
		}
		return jChangeBoxButton;
	}

	/**
	 * This method initializes jChangeWallButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJChangeWallButton() {
		if (jChangeWallButton == null) {
			jChangeWallButton = new JButton();
			jChangeWallButton.setText("Change Wall");
			jChangeWallButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int result = myFiles.showOpenDialog(getJFrame());
					if (result == JFileChooser.APPROVE_OPTION) {
						BufferedImage tmpImage = null;
						try {
							tmpImage = ImageIO.read(myFiles.getSelectedFile());
						} catch (Exception f) {
							JOptionPane.showMessageDialog(getJFrame(), "Reading of image failed!", "ERROR", JOptionPane.ERROR_MESSAGE);
						}
						Field.setWallImage(tmpImage);
						updateWallImage();
						window.repaint();
					}
				}
			});
		}
		return jChangeWallButton;
	}

	/**
	 * This method initializes jChangeFloorButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJChangeFloorButton() {
		if (jChangeFloorButton == null) {
			jChangeFloorButton = new JButton();
			jChangeFloorButton.setText("Change Floor");
			jChangeFloorButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int result = myFiles.showOpenDialog(getJFrame());
					if (result == JFileChooser.APPROVE_OPTION) {
						BufferedImage tmpImage = null;
						try {
							tmpImage = ImageIO.read(myFiles.getSelectedFile());
						} catch (Exception f) {
							JOptionPane.showMessageDialog(getJFrame(), "Reading of image failed!", "ERROR", JOptionPane.ERROR_MESSAGE);
						}
						Field.setFloorImage(tmpImage);
						updateFloorImage();
						window.repaint();
					}
				}
			});
		}
		return jChangeFloorButton;
	}

	/**
	 * This method initializes jChangeHoleButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJChangeHoleButton() {
		if (jChangeHoleButton == null) {
			jChangeHoleButton = new JButton();
			jChangeHoleButton.setText("Change Hole");
			jChangeHoleButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int result = myFiles.showOpenDialog(getJFrame());
					if (result == JFileChooser.APPROVE_OPTION) {
						BufferedImage tmpImage = null;
						try {
							tmpImage = ImageIO.read(myFiles.getSelectedFile());
						} catch (Exception f) {
							JOptionPane.showMessageDialog(getJFrame(), "Reading of image failed!", "ERROR", JOptionPane.ERROR_MESSAGE);
						}
						Field.setHoleImage(tmpImage);
						updateHoleImage();
						window.repaint();
					}
				}
			});
		}
		return jChangeHoleButton;
	}

	/**
	 * This method initializes jRestoreDefaultsButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJRestoreDefaultsButton() {
		if (jRestoreDefaultsButton == null) {
			jRestoreDefaultsButton = new JButton();
			jRestoreDefaultsButton.setText("Restore Defaults");
			jRestoreDefaultsButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Field.setDefaultImages();
					updateFirstAvatarImage();
					updateSecondAvatarImage();
					updateBoxImage();
					updateWallImage();
					updateFloorImage();
					updateHoleImage();
					window.repaint();
				}
			});
		}
		return jRestoreDefaultsButton;
	}

	/**
	 * This method initializes jChangeSecondAvatarButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJChangeSecondAvatarButton() {
		if (jChangeSecondAvatarButton == null) {
			jChangeSecondAvatarButton = new JButton();
			jChangeSecondAvatarButton.setText("Change Avatar 2");
			jChangeSecondAvatarButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int result = myFiles.showOpenDialog(getJFrame());
					if (result == JFileChooser.APPROVE_OPTION) {
						BufferedImage tmpImage = null;
						try {
							tmpImage = ImageIO.read(myFiles.getSelectedFile());
						} catch (Exception f) {
							JOptionPane.showMessageDialog(getJFrame(), "Reading of image failed!", "ERROR", JOptionPane.ERROR_MESSAGE);
						}
						Field.setSecondAvatarImage(tmpImage);
						window.repaint();
						getJFrame().repaint();
						jContentPane.repaint();
						jSecondAvatarImage.repaint();
					}
				}
			});
		}
		return jChangeSecondAvatarButton;
	}


}  //  @jve:decl-index=0:visual-constraint="20,-9"
